package com.linser.gistools.entity.control;

import com.linser.gistools.refactoring.geotools.layer.FeatureLayerNew;
import com.linser.gistools.utils.ColorOperation;
import com.linser.gistools.utils.MapOperation;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Shape;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author: linser
 * @Desc: ListView中的单个列表项实体类
 * @create: 2024-01-21 17:05
 **/
public class ListItem {
    private String name;
    private Layer layer;
    private Color color;
    private Shape layerTypeShape;
    private boolean selected;

    public ListItem(Layer layer) {
        this.layer = layer;
        this.name = layer.getTitle()!=null?layer.getTitle():"未命名";
        //this.color = ColorOperation.fxColorFromAwtColor(Objects.requireNonNull(MapOperation.getLayerFillColor(layer)));
        this.layerTypeShape = MapOperation.getLayerGraphicNode(layer);
        this.selected = layer.isVisible();
    }

    public ListItem(File vectorFile) throws IOException {
        Map<String, Object> params = new HashMap<>();
        params.put("url", vectorFile.toURI().toURL());
        params.put("create spatial index", false);
        params.put("memory mapped buffer", false);
        // 获取源数据的编码
        // 获取文件名
        String fileName = vectorFile.getName();
        // 获取文件后缀
        String fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);
        if(fileExtension.equals("shp")){
            Charset currentFileCharset = MapOperation.getShapefileCharset(vectorFile.getPath());
            params.put("charset", currentFileCharset.name());
        }else {
            params.put("charset", "utf-8");
        }
        DataStore store = DataStoreFinder.getDataStore(params);
        SimpleFeatureSource featureSource = store.getFeatureSource(store.getTypeNames()[0]);
        java.awt.Color randomColor = new java.awt.Color((int) (Math.random() * 0x1000000));
        Style style = SLD.createSimpleStyle(featureSource.getSchema(),randomColor);

        String nameWithoutExtension = fileName.substring(0, fileName.lastIndexOf('.'));
        Layer layer = new FeatureLayerNew(featureSource, style,nameWithoutExtension);

        this.layer = layer;
        this.name = layer.getTitle()!=null?layer.getTitle():"未命名";
        this.layerTypeShape = MapOperation.getLayerGraphicNode(layer);
        this.selected = layer.isVisible();
    }

    public String getName() {
        return name;
    }

    public Color getColor() {
        return color;
    }

    public Layer getLayer() {
        return layer;
    }

    public boolean isSelected() {
        return selected;
    }

    public String toString(){
        return name;
    }

    public void setSelected(boolean selected) {
        this.layer.setVisible(selected);
        this.selected = selected;
    }

    public Shape getLayerTypeShape() {
        return layerTypeShape;
    }

    public void setLayerTypeShape(Shape layerTypeShape) {
        this.layerTypeShape = layerTypeShape;
    }
}
